package com.eastfair.imaster.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.eastfair.api.QuestionnaireServiceFeign;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.core.base.R;
import com.eastfair.core.jackson.JsonUtil;
import com.eastfair.core.utils.ApacheHttpClientsUtils;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.enums.SystemSourceEnum;
import com.eastfair.imaster.dao.VisitorRegisterMapper;
import com.eastfair.imaster.dao.VisitorUserMapper;
import com.eastfair.imaster.dto.VisitorRegisterDTO;
import com.eastfair.imaster.dto.VisitorRegisterStateDTO;
import com.eastfair.imaster.entity.*;
import com.eastfair.imaster.enumeration.BZTSourceEnum;
import com.eastfair.imaster.service.CompanyService;
import com.eastfair.imaster.service.TccService;
import com.eastfair.imaster.service.VisitorRegisterPushService;
import com.eastfair.imaster.service.VisitorRegisterStateService;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.questionnaire.dto.AnswerSubjectResultSaveDTO;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.rm.tcc.api.BusinessActionContextParameter;
import io.seata.rm.tcc.api.TwoPhaseBusinessAction;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * Date:2021/8/24
 * Time:11:19
 *
 * @author zjn
 */
@Slf4j
@Service
@DS("#thread.tenant")
@Transactional
public class TccServiceImpl implements TccService {

    @Autowired
    CompanyService companyService;


    @Override
    @GlobalTransactional
//    @GlobalTransactional(name = "executeTcc" , rollbackFor = Exception.class)
    public R executeTcc(Long id) {
        String xid = RootContext.getXID();
        System.out.println("TccActionTwo prepare, xid:" + xid + ", id:" + id);
        executeTest(null, id, "test");
        int i = 1;
        if (i == 1) {
            throw new RuntimeException("公司不存在");
        }
        return null;
    }


    @Override
//    @GlobalTransactional
//    @GlobalTransactional(name = "executeTest", rollbackFor = Exception.class)
//    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public R executeTest(BusinessActionContext actionContext, Long id, String addName) {
        String xid = RootContext.getXID();
        System.out.println("TccActionTwo prepare, xid:" + xid + ", id:" + id);
        Company company = companyService.getById(id);
//        if(company==null){
//            throw new RuntimeException("公司不存在");
//        }
        //锁定金额
//        if(company!=null){
//            company.setName(addName+company.getName());
//        }
//        companyService.updateById(company);
        return R.success();
    }

    @Override
    public boolean commit(BusinessActionContext actionContext) {
        if (actionContext.getXid() == null) {
            return true;
        }
        log.info("commit is execute");
        Long id = Long.valueOf(actionContext.getActionContext("id").toString());
        String addName = actionContext.getActionContext("addName").toString();
        Company company = companyService.getById(id);
        if (company != null) {
            company.setName(addName + company.getName());
        }
        companyService.updateById(company);
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext actionContext) {
        log.info("rollback is execute");
        if (actionContext.getXid() == null) {
            return true;
        }
        log.info("rollback is execute");
        return true;
    }
}
